﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using fcwms.Common;

namespace fcwms.DAL
{
    /// <summary>
    /// 数据访问类:dt_goods
    /// </summary>
    public partial class dt_goods
    {
        public dt_goods()
        { }
        #region  BasicMethod
        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from dt_goods");
            strSql.Append(" where id=@id");
            SqlParameter[] parameters = {
					new SqlParameter("@id", SqlDbType.Int,4)
			};
            parameters[0].Value = id;

            return DbHelperSQL.Exists(strSql.ToString(), parameters);
        }


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(fcwms.Model.dt_goods model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into dt_goods(");
            strSql.Append("decla_id,position,goods_name,num,single_num,cube,storage_num,days,cost,cost_status,deal_status,remark)");
            strSql.Append(" values (");
            strSql.Append("@decla_id,@position,@goods_name,@num,@single_num,@cube,@storage_num,@days,@cost,@cost_status,@deal_status,@remark)");
            strSql.Append(";select @@IDENTITY");
            SqlParameter[] parameters = {
					new SqlParameter("@decla_id", SqlDbType.NVarChar,100),
					new SqlParameter("@goods_name", SqlDbType.NVarChar,100),
					new SqlParameter("@num", SqlDbType.Decimal,9),
					new SqlParameter("@single_num", SqlDbType.Decimal,9),
					new SqlParameter("@cube", SqlDbType.Decimal,9),
					new SqlParameter("@storage_num", SqlDbType.Decimal,9),
					new SqlParameter("@days", SqlDbType.Int,4),
					new SqlParameter("@cost", SqlDbType.Decimal,9),
					new SqlParameter("@cost_status", SqlDbType.Int,4),
					new SqlParameter("@deal_status", SqlDbType.Int,4),
					new SqlParameter("@remark", SqlDbType.NVarChar,200),
                    new SqlParameter("@position",SqlDbType.NVarChar,50)};
            parameters[0].Value = model.decla_id;
            parameters[1].Value = model.goods_name;
            parameters[2].Value = model.num;
            parameters[3].Value = model.single_num;
            parameters[4].Value = model.cube;
            parameters[5].Value = model.storage_num;
            parameters[6].Value = model.days;
            parameters[7].Value = model.cost;
            parameters[8].Value = model.cost_status;
            parameters[9].Value = model.deal_status;
            parameters[10].Value = model.remark;
            parameters[11].Value = model.position;

            object obj = DbHelperSQL.GetSingle(strSql.ToString(), parameters);
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(fcwms.Model.dt_goods model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update dt_goods set ");
            strSql.Append("decla_id=@decla_id,");
            strSql.Append("position=@position,");
            strSql.Append("goods_name=@goods_name,");
            strSql.Append("num=@num,");
            strSql.Append("single_num=@single_num,");
            strSql.Append("cube=@cube,");
            strSql.Append("storage_num=@storage_num,");
            strSql.Append("days=@days,");
            strSql.Append("cost=@cost,");
            strSql.Append("cost_status=@cost_status,");
            strSql.Append("deal_status=@deal_status,");
            strSql.Append("remark=@remark");
            strSql.Append(" where id=@id");
            SqlParameter[] parameters = {
					new SqlParameter("@decla_id", SqlDbType.NVarChar,100),
					new SqlParameter("@goods_name", SqlDbType.NVarChar,100),
					new SqlParameter("@num", SqlDbType.Decimal,9),
					new SqlParameter("@single_num", SqlDbType.Decimal,9),
					new SqlParameter("@cube", SqlDbType.Decimal,9),
					new SqlParameter("@storage_num", SqlDbType.Decimal,9),
					new SqlParameter("@days", SqlDbType.Int,4),
					new SqlParameter("@cost", SqlDbType.Decimal,9),
					new SqlParameter("@cost_status", SqlDbType.Int,4),
					new SqlParameter("@deal_status", SqlDbType.Int,4),
					new SqlParameter("@remark", SqlDbType.NVarChar,200),
					new SqlParameter("@id", SqlDbType.Int,4),
                    new SqlParameter("@position",SqlDbType.NVarChar,50)};
            parameters[0].Value = model.decla_id;
            parameters[1].Value = model.goods_name;
            parameters[2].Value = model.num;
            parameters[3].Value = model.single_num;
            parameters[4].Value = model.cube;
            parameters[5].Value = model.storage_num;
            parameters[6].Value = model.days;
            parameters[7].Value = model.cost;
            parameters[8].Value = model.cost_status;
            parameters[9].Value = model.deal_status;
            parameters[10].Value = model.remark;
            parameters[11].Value = model.id;
            parameters[12].Value = model.position;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int id)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from dt_goods ");
            strSql.Append(" where id=@id");
            SqlParameter[] parameters = {
					new SqlParameter("@id", SqlDbType.Int,4)
			};
            parameters[0].Value = id;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 批量删除数据
        /// </summary>
        public bool DeleteList(string idlist)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from dt_goods ");
            strSql.Append(" where id in (" + idlist + ")  ");
            int rows = DbHelperSQL.ExecuteSql(strSql.ToString());
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

       

        /// <summary>
        /// 查找不存在的菜单并删除
        /// </summary>
        public void DeleteList2(SqlConnection conn, SqlTransaction trans, List<Model.dt_goods> models, string decla_id)
        {
            StringBuilder idList = new StringBuilder();
            if (models != null)
            {
                foreach (Model.dt_goods modelt in models)
                {
                    if (modelt.id > 0)
                    {
                        idList.Append(modelt.id + ",");
                    }
                }
            }
            string id_list = Utils.DelLastChar(idList.ToString(), ",");
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from dt_goods where decla_id=" + decla_id);
            if (!string.IsNullOrEmpty(id_list) && id_list != "0")
            {
                strSql.Append(" and id not in(" + id_list + ")");
            }
            DbHelperSQL.ExecuteSql(conn, trans, strSql.ToString());
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public fcwms.Model.dt_goods GetModel(int id)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 id,decla_id,position,goods_name,num,single_num,cube,storage_num,days,cost,cost_status,deal_status,remark from dt_goods ");
            strSql.Append(" where id=@id");
            SqlParameter[] parameters = {
					new SqlParameter("@id", SqlDbType.Int,4)
			};
            parameters[0].Value = id;

            fcwms.Model.dt_goods model = new fcwms.Model.dt_goods();
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                return DataRowToModel(ds.Tables[0].Rows[0]);
            }
            else
            {
                return null;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public fcwms.Model.dt_goods DataRowToModel(DataRow row)
        {
            fcwms.Model.dt_goods model = new fcwms.Model.dt_goods();
            if (row != null)
            {
                if (row["id"] != null && row["id"].ToString() != "")
                {
                    model.id = int.Parse(row["id"].ToString());
                }
                if (row["decla_id"] != null)
                {
                    model.decla_id = row["decla_id"].ToString();
                }
                if (row["position"] != null)
                {
                    model.position = row["position"].ToString();
                }
                if (row["goods_name"] != null)
                {
                    model.goods_name = row["goods_name"].ToString();
                }
                if (row["num"] != null && row["num"].ToString() != "")
                {
                    model.num = decimal.Parse(row["num"].ToString());
                }
                if (row["single_num"] != null && row["single_num"].ToString() != "")
                {
                    model.single_num = decimal.Parse(row["single_num"].ToString());
                }
                if (row["cube"] != null && row["cube"].ToString() != "")
                {
                    model.cube = decimal.Parse(row["cube"].ToString());
                }
                if (row["storage_num"] != null && row["storage_num"].ToString() != "")
                {
                    model.storage_num = decimal.Parse(row["storage_num"].ToString());
                }
                if (row["days"] != null && row["days"].ToString() != "")
                {
                    model.days = int.Parse(row["days"].ToString());
                }
                if (row["cost"] != null && row["cost"].ToString() != "")
                {
                    model.cost = decimal.Parse(row["cost"].ToString());
                }
                if (row["cost_status"] != null && row["cost_status"].ToString() != "")
                {
                    model.cost_status = int.Parse(row["cost_status"].ToString());
                }
                if (row["deal_status"] != null && row["deal_status"].ToString() != "")
                {
                    model.deal_status = int.Parse(row["deal_status"].ToString());
                }
                if (row["remark"] != null)
                {
                    model.remark = row["remark"].ToString();
                }
            }
            return model;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select id,decla_id,position,goods_name,num,single_num,cube,storage_num,days,cost,cost_status,deal_status,remark ");
            strSql.Append(" FROM dt_goods ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ");
            if (Top > 0)
            {
                strSql.Append(" top " + Top.ToString());
            }
            strSql.Append(" id,decla_id,goods_name,num,single_num,cube,storage_num,days,cost,cost_status,deal_status,remark ");
            strSql.Append(" FROM dt_goods ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by " + filedOrder);
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获取记录总数
        /// </summary>
        public int GetRecordCount(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) FROM dt_goods ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            object obj = DbHelperSQL.GetSingle(strSql.ToString());
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("SELECT * FROM ( ");
            strSql.Append(" SELECT ROW_NUMBER() OVER (");
            if (!string.IsNullOrEmpty(orderby.Trim()))
            {
                strSql.Append("order by T." + orderby);
            }
            else
            {
                strSql.Append("order by T.id desc");
            }
            strSql.Append(")AS Row, T.*  from dt_goods T ");
            if (!string.IsNullOrEmpty(strWhere.Trim()))
            {
                strSql.Append(" WHERE " + strWhere);
            }
            strSql.Append(" ) TT");
            strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
            return DbHelperSQL.Query(strSql.ToString());
        }
        #endregion  BasicMethod
        #region  ExtensionMethod

        #endregion  ExtensionMethod
    }
}
